package com.blood.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blood.core.resp.GlobalResultStatus;
import com.blood.core.resp.ResponseResult;
import com.blood.modules.system.dto.req.CheckAppVersionReq;
import com.blood.modules.system.dto.resp.CheckAppVersionResp;
import com.blood.modules.system.mapper.AppVersionMapper;
import com.blood.modules.system.model.AppVersion;
import com.blood.modules.system.service.ISysService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author daoren
 * @date 2020/8/14 14:56
 * @description 系统模块 服务
 */
@Slf4j
@Service
public class SysServiceImpl implements ISysService {

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Override
    public ResponseResult<CheckAppVersionResp> checkAppVersion(CheckAppVersionReq req) {
        CheckAppVersionResp resp = new CheckAppVersionResp();
        //获取数据库当前最新版本
        AppVersion newVersion = findNewAppVersionByType(req.getServiceType(), req.getType());
        if (null == newVersion) {
            resp.setNewVersion(false);
        } else {
            BeanUtils.copyProperties(newVersion, resp);
            resp.setNewVersion(newVersion.getVersion()>req.getVersion());
        }
        return GlobalResultStatus.SUCCESS.toResult().response(resp);
    }

    /**
     * 获取该类型当前最新版本
     *
     * @param type app类型
     * @return
     */
    public AppVersion findNewAppVersionByType(String serviceType, Integer type) {
        QueryWrapper<AppVersion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AppVersion::getServiceType, serviceType)
                .eq(AppVersion::getType, type)
                .eq(AppVersion::isDeleteFlag, false)
                .orderByDesc(AppVersion::getLineNo)
                .last("limit 1");
        return appVersionMapper.selectOne(wrapper);
    }


}
